package com.gitee.mrj.aiboot.core.sf.generators.ui.widget;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.gitee.mrj.aiboot.annotation.config.sf.ui.widget.SFAutoCompleteWidget;
import com.gitee.mrj.aiboot.core.sf.generators.FormDefinitionGenerator;
import com.gitee.mrj.aiboot.core.sf.generators.UIForm;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

@Component
public class SFAutoCompleteWidgetGenerator implements FormDefinitionGenerator {
    @Override
    public void generate(UIForm root, Field field, UIForm node) {
        SFAutoCompleteWidget completeWidget = AnnotatedElementUtils.findMergedAnnotation(field, SFAutoCompleteWidget.class);
        generate(node, completeWidget);
    }

    private void generate(UIForm node, SFAutoCompleteWidget completeWidget) {
        ObjectNode ui = node.getUi();
        ui.put("widget", "autocomplete");
        putString(ui, "placeholder", completeWidget.placeholder());
        ui.put("filterOption", completeWidget.filterOption());
        putInt(ui, "debounceTime", completeWidget.debounceTime());
        ui.put("defaultActiveFirstOption", completeWidget.defaultActiveFirstOption());
        ui.put("backfill", completeWidget.backfill());

        putInt(ui, "nzWidth", completeWidget.nzWidth());

    }


    @Override
    public Class[] supports() {
        return new Class[]{SFAutoCompleteWidget.class};
    }
}
